package com.blb.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blb.ssm.entity.Supplier;
import com.blb.ssm.service.SupplierService;

@Controller
@RequestMapping("/supplier")
public class SupplierController {
	
	@Autowired
	private SupplierService supplierService;
	
	@RequestMapping("")
	public String test(){
		return "supplier";
	}
	
	/**
	 * 查询全部
	 * 条件查询
	 * @param scom
	 * @param stel
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/list")
	public Map<String,Object> getSupplierlist(String scom,String stel,int page,int limit) throws Exception{
		//System.out.println(scom+" "+stel);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Supplier> slist=supplierService.getlist(scom,stel,page,limit);
		int count=supplierService.getCount(scom,stel);
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", slist);
		return map;
	}
	
	/**
	 * 删除
	 * @param sid
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del")
	public String deleteSupplier(String sid){
		try {
			supplierService.delSupplier(sid);
			return "ok";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * 新增
	 * @param supplier
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,Object> addSupplier(Supplier supplier){
		Map<String, Object> map = new HashMap<String,Object>();
		String msg=null;
		try {
			//System.out.println(supplier);
			supplier.setSid(UUID.randomUUID().toString());
			msg=supplierService.addSupplier(supplier);
			map.put("code", "success");
			map.put("msg", msg);
		} catch (Exception e) {
			map.put("code", "error");
			e.printStackTrace();
		}
		return map;
	}
	
	/**
	 * 更新
	 * @param supplier
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Map<String,Object> updateSupplier(Supplier supplier){
		Map<String, Object> map = new HashMap<String,Object>();
		String msg=null;
		try {
//			System.out.println(supplier);
			msg=supplierService.updateSupplier(supplier);
			map.put("code", "success");
			map.put("msg", msg);
		} catch (Exception e) {
			map.put("code", "error");
			e.printStackTrace();
		}
		return map;
	}
}
